package com.example.elemboot.Controller;

import com.example.elemboot.Dao.IBusiness;
import com.example.elemboot.Dao.ICart;
import com.example.elemboot.Dao.IUser;
import com.example.elemboot.Service.ICartService;
import com.example.elemboot.Utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cart")
public class CartController {


    @Resource
    private ICartService cartService;

    @PostMapping(value = "/saveCart")
    public Result saveCart(
            @RequestParam("userPhone") String userPhone,
            @RequestParam("businessId") Integer businessID,
            @RequestParam("foodId") Integer foodId,
            @RequestParam("quantity") Integer quantity
    ) {
        try {
            if (cartService.saveCart(userPhone, businessID, foodId, quantity)) {
                return Result.success("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
        return Result.error("添加失败");
    }

    //更新购物车
    @PostMapping(value = "/updateCart")
    public Result updateCart(@RequestParam("userPhone") String userPhone,
                             @RequestParam("businessId") Integer businessID,
                             @RequestParam("foodId") Integer foodId,
                             @RequestParam("quantity") Integer quantity){
        try {
            if (cartService.updateCart(userPhone, businessID, foodId, quantity)) {
                return Result.success("更新成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
        return Result.error("更新失败");
    }

    //删除购物车记录
    @PostMapping(value = "/removeCartItem")
    public Result removeCartItem(@RequestParam("userPhone") String userPhone,
                             @RequestParam("businessId") Integer businessID,
                             @RequestParam("foodId") Integer foodId,
                             @RequestParam("quantity") Integer quantity){
        try {
            if (cartService.removeCartItem(userPhone, businessID, foodId, quantity)) {
                return Result.success("更新成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
        return Result.error("更新失败");
    }

    //获取购物车
    @PostMapping(value = "/listCart")
    public Result listCart(@RequestParam("userPhone") String userPhone,
                           @RequestParam("businessId") Integer businessID){
        try {
            return Result.success(cartService.getCartList(userPhone, businessID));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }

}
